package cn.yunhe.controller;

import cn.yunhe.entity.Course;
import cn.yunhe.entity.Lib;
import cn.yunhe.entity.Point;
import cn.yunhe.services.ILibServices;
import cn.yunhe.services.IPointServices;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 王雪丹 on 2018/5/25.
 */
@RestController
@RequestMapping("/point")
public class PointController {
    @Resource
    private IPointServices pointServices;

    @RequestMapping("/list_point")
    public ModelAndView list_point(){
        return new ModelAndView("ftl/list_point");
    }

    @RequestMapping("/getPointPage")
    public Map queryListLikePoint(int pageSize, int pageNum, Point point){
        Map map = new HashMap<>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("point",point);
        Page page = pointServices.queryListLikePoint(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }
    @RequestMapping("/toAddPoint")
    public ModelAndView getAllLib(){
        List<Lib> libs = pointServices.getAllLib();
        List<Course> courses = pointServices.getAllCourse();
        /*session.setAttribute("libs",libs);
        session.setAttribute("courses",courses);*/
        ModelAndView mv = new ModelAndView();
        mv.addObject("libs",libs);
        mv.addObject("courses",courses);
        mv.setViewName("ftl/addPoint");
        return mv;
    }

    @RequestMapping("/addPoint")
    public int addPoint(Point point){
        return pointServices.addPoint(point);
    }

    @RequestMapping("/toUpadtePoint")
    public ModelAndView toUpadteLib(Point point){
        List<Lib> libs = pointServices.getAllLib();
        List<Course> courses = pointServices.getAllCourse();
        point = pointServices.getById(point);
        ModelAndView mv = new ModelAndView();
        mv.addObject("libs",libs);
        mv.addObject("courses",courses);
        mv.addObject("point",point);
        mv.setViewName("ftl/toUpadtePoint");
        return mv;
    }
    @RequestMapping("/updatePoint")
    public int updatePoint(Point point){
        return pointServices.updatePoint(point);
    }

    public IPointServices getPointServices() {
        return pointServices;
    }

    public void setPointServices(IPointServices pointServices) {
        this.pointServices = pointServices;
    }


}
